<template>
  <div class="app-container">
    <SearchForm
      v-show="showSearch"
      v-model="queryParams"
      :options="options.formOps"
      @onQuery="getList"
    >
      <template #default="{ filterForm, handleQuery }">
        <el-col :span="6">
          <el-form-item label="重大危险源名称" prop="fileName">
            <el-input
              @keyup.enter="handleQuery"
              @change="handleQuery"
              v-model="filterForm.dangerName"
              maxlength="30"
            />
          </el-form-item>
        </el-col>
      </template>
    </SearchForm>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          >新增</el-button
        >
      </el-col>

      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <Table
      ref="table"
      :auto-featch="true"
      :fetch-data="fetchData"
      :options="options.table"
    >
      <template #beforecheckTime>
        <el-table-column
          label="检查部门"
          align="center"
          key="checkDept"
          prop="checkDept"
        />
        <el-table-column
          label="重大危险源名称"
          align="center"
          key="dangerName"
          prop="dangerName"
        />
      </template>
      <template #default>
        <el-table-column
          label="上传人"
          align="center"
          key="uploadName"
          prop="uploadName"
        />
        <el-table-column
          label="上传时间"
          align="center"
          key="createTime"
          prop="createTime"
          width="200"
        />
        <el-table-column
          label="创建人"
          align="center"
          key="createUserName"
          prop="createUserName"
        />
      </template>
      <template #actionButton="{ row }">
        <el-button type="text" icon="download" @click="handDownload(row)"
          >下载</el-button
        >
        <el-button type="text" icon="Edit" @click="handleEdit(row)"
          >修改</el-button
        >

        <el-button
          type="text"
          icon="Delete"
          style="color: red"
          @click="handleDel(row)"
          >删除</el-button
        >
      </template></Table
    >
    <DialogVform
      :formJson="formJson"
      ref="dialogVform"
      @onSubmit="onSubmit"
      :slots="['slotDefault']"
      :title="dialogType == 'add' ? '新增履职记录' : '修改履职记录'"
    >
      <template #slotDefault>
        <el-row :gutter="12">
          <el-col :span="12">
            <el-form-item label="检查部门" prop="fileName">
              <el-select style="width: 100%" v-model="form.checkDept" clearable>
                <el-option
                  v-for="dict in dangerUnit"
                  :key="dict.deptName"
                  :label="dict.deptName"
                  :value="dict.deptName"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="重大危险源名称" prop="fileName">
              <el-select
                style="width: 100%"
                v-model="form.dangerName"
                clearable
              >
                <el-option
                  v-for="dict in dangerUnitName"
                  :key="dict"
                  :label="dict"
                  :value="dict"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="12">
          <el-col :span="24">
            <el-form-item label="附件上传" prop="fileId">
              <FileUpload
                :limit="1"
                :fileSize="30"
                :fileType="['doc', 'docx', 'pdf']"
                v-model="files"
              ></FileUpload>
            </el-form-item>
          </el-col>
        </el-row>
      </template>
    </DialogVform>
  </div>
</template>

<script setup>
import Table from "@/components/Table/index.vue";
import SearchForm from "@/components/searchForm/index";
import DialogVform from "@/components/dialogVform/index";
import { lvzhijilu as APIS } from "@/api/hdanger/lvzhijilu/index.js";
import FileUpload from "@/components/FileUpload";
import formJson from "./components/options.json";
import { useAsyncData, useFormJson } from "@/views/hooks/useAsyncData.js";
const ops = useFormJson(formJson, {
  sort: [],
  sortable: [],
});
const { proxy } = getCurrentInstance();
const showSearch = ref(true);

const options = reactive({
  table: ops,
  formOps: ops.filter((i) => ["dangerName", "dangerLevel"].includes(i.prop)),
});
const dangerUnit = ref([]);
const dangerUnitName = ref([]);
const files = ref([]);
const table = ref();
const dialogVform = ref();
watch(
  () => files,
  (newData) => {
    console.log(newData);
    if (newData.value.length === 0) {
      form.value.fileName = "";
      form.value.fileId = "";
      form.value.fileUrl = "";
    } else {
      form.value.fileName = newData.value[0].name;
      form.value.fileId = newData.value[0].id;
      form.value.fileUrl = newData.value[0].url;
    }
  },
  {
    deep: true,
  }
);
const plugEdit = (row) => {
  form.value.checkDept = row.checkDept;
  form.value.dangerName = row.dangerName;
  if (row.fileId) {
    files.value = [
      {
        name: row.fileName,
        id: row.fileId,
        url: row.fileUrl,
      },
    ];
  } else {
    files.value = [];
  }
};
const handDownload = (row) => {
  window.downLoadFile(row.fileUrl, row.fileName);
};
const {
  onSubmit,
  fetchData,
  dialogType,
  handleAdd,
  handleEdit,
  getList,
  handleDel,
  queryParams,
  form,
} = useAsyncData({
  APIS,
  formJson,
  dialogVform,
  table,
  proxy,
  plugEdit,
});

onMounted(() => {
  APIS.getAllDept().then((res) => {
    dangerUnit.value = res.data;
    dangerUnitName.value = res.data.map((item) => {
      return item.dangerName.join("、");
    });
    console.log(res);
  });
});
</script>

<style lang="scss" scoped></style>
